package com.example.letoprometey;

import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.webkit.WebView;
import android.widget.TextView;

import com.example.letoprometey.adapters.FragmentAdapter;
import com.viewpagerindicator.CirclePageIndicator;
import com.viewpagerindicator.PageIndicator;

public class WalkThroughActivity extends FragmentActivity {
	private FragmentAdapter mAdapter;
	private ViewPager mPager;
	private PageIndicator mIndicator;
	private ArrayList<Fragment> mFragments;

	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.activity_walkthrough);

		init();
	}

	private void init() {

		getWindow().setSoftInputMode(
				WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

		mPager = (ViewPager) findViewById(R.id.pager);
		mFragments = new ArrayList<Fragment>();

		getFragments();

		mAdapter = new FragmentAdapter(getSupportFragmentManager(), mFragments);
		mPager.setAdapter(mAdapter);

		mIndicator = (CirclePageIndicator) findViewById(R.id.indicator);
		mIndicator.setViewPager(mPager);

		mIndicator.setOnPageChangeListener(new OnPageChangeListener() {

			@Override
			public void onPageSelected(int arg0) {
				if (arg0 == mFragments.size() - 1) {
					showMyDialog();
				}
			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onPageScrollStateChanged(int arg0) {
				// TODO Auto-generated method stub

			}
		});
	}

	private void showMyDialog() {
		final Dialog dialog = new Dialog(this);
		dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
		dialog.setContentView(R.layout.layout_dialog_5_17_21);

		Window window = dialog.getWindow();
		WindowManager.LayoutParams wlp = window.getAttributes();

		wlp.gravity = Gravity.CENTER;
		wlp.width = WindowManager.LayoutParams.FILL_PARENT;
		wlp.flags &= WindowManager.LayoutParams.FLAG_DIM_BEHIND;
		window.setAttributes(wlp);

		dialog.getWindow().setAttributes(wlp);

		dialog.show();

		TextView buttonOK = (TextView) dialog.findViewById(R.id.button_ok);
		buttonOK.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				dialog.dismiss();
			}
		});
	}

	private void getFragments() {
		mFragments.add(createFragment(R.layout.fragment_pane_5_20));
		mFragments.add(createFragment(R.layout.fragment_pane_6_7));
		mFragments.add(createFragment(R.layout.fragment_pane_8));
		mFragments.add(createFragment(R.layout.fragment_pane_9));
		mFragments.add(createFragment(R.layout.fragment_pane_10));
		mFragments.add(createFragment(R.layout.fragment_pane_11));
		mFragments.add(createFragment(R.layout.fragment_pane_12));
		mFragments.add(createFragment(R.layout.fragment_pane_13_14));
		mFragments.add(createFragment(R.layout.fragment_pane_15));
		mFragments.add(createFragment(R.layout.fragment_pane_16));
		mFragments.add(createFragment(R.layout.fragment_pane_17));
		mFragments.add(createFragment(R.layout.fragment_pane_18_19));
		mFragments.add(createFragment(R.layout.fragment_pane_22));
		mFragments.add(createFragment(R.layout.fragment_pane_23));
	}

	@SuppressLint("ValidFragment")
	private Fragment createFragment(final int res) {
		return new Fragment() {
			@Override
			public View onCreateView(LayoutInflater inflater,
					ViewGroup container, Bundle savedInstanceState) {
				View v = inflater.inflate(res, container, false);
				if (res == R.layout.fragment_pane_9) {
					WebView mWebView = (WebView) v.findViewById(R.id.webview_9);
					mWebView.loadUrl("file:///android_asset/test.html");
				}
				return v;
			}
		};
	}

}
